/**
 * Created by dbwyxl on 2017/9/19.
 */
module.exports =function($cookies,$scope, $rootScope, $state,contractLibraryService,$timeout){


  //获取列表
  $scope.getContractLibrary=getContractLibrary;
  //模糊查询
  $scope.getFindTemplateList=getFindTemplateList;
  //开关
  $scope.conListClassToggle=conListClassToggle;
  //分类list
  $scope.queryDictTreeData=queryDictTreeData;
  //确认 筛选
  $scope.contractAffirmClassify=contractAffirmClassify;
  //服务类型 确认
  $scope.contractAffirmClassifyC=contractAffirmClassifyC;


  $scope.contractLi={
    pageSize:10,
    beginNum:1,
    keyword:'',
    total:'',
    parentId:'',
    templateDictId:'',
    classifyChildren:''
  }
  $scope.init=function(){
    $scope.getContractLibrary();
    initQueryDictTreeData()
  }
  $scope.init()



  function getContractLibrary(){
    var params={
      keyword:$scope.contractLi.keyword.replace(/(^\s*)|(\s*$)/g, ""),
      templateDictId:$scope.contractLi.templateDictId,
      pageSize:$scope.contractLi.pageSize,
      beginNum:$scope.contractLi.beginNum
    }
    contractLibraryService.findTemplatePageList(params).then(function(data){
      if(data.code==1){
        $scope.contractLi.contractList=data.data;
        $scope.contractLi.total=data.total;
        $scope.contractLi.hintContent='';
        var maxCHeight = $(window).height()-129-angular.element('#contractLibrary_box').height()-10+ 'px';
        $('.conList_item').css('height',maxCHeight);
      }

    })
  }

  function getFindTemplateList(){
    if($scope.contractLi.keyword.length>=2){
      contractLibraryService.findTemplateList($scope.contractLi.keyword).then(function(data){
        if(data.code==1){
          $scope.contractLi.hintContent=data.data;
          $scope.contractLi.templateDictId='';
        }
      })
    }else{
      $scope.contractLi.hintContent='';
    }

  }

  function conListClassToggle(){
    $timeout(function(){
      var maxCHeight = $(window).height()-129-angular.element('#contractLibrary_box').height()-5+ 'px';
      $('.conList_item').css('height',maxCHeight);
    },1)
    $scope.conListClassifyToggle=!$scope.conListClassifyToggle;
  }

  function queryDictTreeData(parentId,name){
    if(parentId!=$scope.contractLi.parentId){
      $scope.contractLi.parentId=parentId;
      $scope.contractLi.parentName=name;
      contractLibraryService.queryDictTreeData(parentId).then(function(data){
        if(data.code==1){
          $scope.contractLi.sevreClassify=data.data;
          $timeout(function(){
            var maxCHeight = $(window).height()-129-angular.element('#contractLibrary_box').height()-5+ 'px';
            $('.conList_item').css('height',maxCHeight);
          },1);
          $scope.contractLi.classifyChildren='';
          $scope.contractLi.keyword='';
          parentId==''?$scope.contractLi.sevreClassify='':'';
          $scope.contractLi.templateDictId=parentId;
          $scope.getContractLibrary()
        }

      })
    }
  }
  function contractAffirmClassify(data){

    if(data){
      $scope.contractLi.keyword=data.contractName;

    }
    $scope.contractLi.parentId='';
    $scope.contractLi.classifyChildren='';
    $scope.contractLi.sevreClassify='';
    $scope.conListClassifyToggle=false;
    $scope.contractLi.templateDictId='';
    $scope.getContractLibrary();

  }
  function contractAffirmClassifyC(data){
    if(data){
      $scope.contractLi.classifyChildren=data.name;
      $scope.contractLi.templateDictId=data.id;
    }else{
      $scope.contractLi.classifyChildren='';
      $scope.contractLi.templateDictId=$scope.contractLi.parentId;
    }
    $scope.getContractLibrary()
  }

  function initQueryDictTreeData(){
    contractLibraryService.queryDictTreeData(1).then(function(data){
      if(data.code==1){
        $scope.contractLi.fatSevreClassify=data.data;
      }
    })
  }

  //下载附件
  $scope.downloadFileProgress = function (file, key) {
    /*taskId
     taskAttachedId*/
    var params = {'contractId':file.id, 'fileName':file.contractName};
    contractLibraryService.tempDownload(params).then(function (response) {
      var fileData = new Blob([response.data]);
      var link = document.createElement('a');
      link.href = URL.createObjectURL(fileData);
      link.download = file.contractName;
      link.click();
      // window.URL.revokeObjectURL(link.href);
    });
    this.contractLi.contractList[key].downloadCount++;
  }



//添加收藏
  $scope.addCollect = function (obj, index) {
    var params={'templateId':obj.id, 'typeName': obj.typeName, 'parentName': obj.parentName}
    contractLibraryService.insertUserTemplateCollect(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.contractLi.contractList[index].isCollection = 1;
      }
    }, function (response) {
    });
  }

  //取消收藏
  $scope.delCollect = function (obj, index) {
    var params={'templateId':obj.id}
    contractLibraryService.deleteUserTemplateCollect(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.contractLi.contractList[index].isCollection = 0;
      }
    }, function (response) {
    });
  }


};
